//
//  HomeDQHYMViewController.m
//  shuiyun
//
//  Created by 马田成 on 2017/9/7.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeDQHYMViewController.h"
#import "HomeDQHYMCollectionViewCell.h"
#import "HomeDQHYListViewController.h"

@interface HomeDQHYMViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *area;
@property (nonatomic, strong) NSMutableArray *count;


@end

@implementation HomeDQHYMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationData];
    
    [self setTitle:@"地区货源"];
    [self configLayout];
    
    // Do any additional setup after loading the view.
}
- (void)configLayout{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    layout.itemSize = CGSizeMake((DEF_SCREEN_WIDTH - 70) / 4, 40);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, DEF_SCREEN_WIDTH - 20, DEF_SCREEN_HEIGHT - 74) collectionViewLayout:layout];
   
    //
    [self.collectionView layoutIfNeeded];
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.pagingEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeDQHYMCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HomeDQHYMCollectionViewCell"];
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    //    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
}
- (void)creationData{

    self.area = [NSMutableArray array];
    self.count = [NSMutableArray array];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    NSString *userid = [PublicFunction getConfigWithKey:@"userid"];
    
    [productParams setValue:userid forKey:@"userid"];
    
    [productParams setValue:token forKey:@"token"];

    
    [[MNetworkHandler sharedInstance]conURL:hometGoodsArea networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            NSMutableDictionary *dic = [returnData objectForKey:@"data"];
            
            [self.view addSubview:self.collectionView];
            self.area = [dic objectForKey:@"area"];
            self.count = [dic objectForKey:@"count"];
            
            
            
            [self.collectionView reloadData];
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];
    }];

}
//类别
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}
//数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.area.count;
    
    
}
//内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeDQHYMCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeDQHYMCollectionViewCell" forIndexPath:indexPath];
    
    NSString *str1 = @"(";
    NSString *str2 = @")";
    cell.titleLabel.text = [[[self.area[indexPath.row] stringByAppendingString:str1] stringByAppendingString:self.count[indexPath.row]] stringByAppendingString:str2];
 
    return cell;
    
    
    
}
//点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

//    [self.delegate getArea:self.area[indexPath.row]];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    NSString *userid = [PublicFunction getConfigWithKey:@"userid"];
    
    [productParams setValue:userid forKey:@"userid"];
    [productParams setValue:self.area[indexPath.row] forKey:@"area"];
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:@"1" forKey:@"p"];
    
    [[MNetworkHandler sharedInstance]conURL:hometGoodsindex networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {

           
            HomeDQHYListViewController *vc= [[HomeDQHYListViewController alloc] init];
            vc.areaStr =self.area[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
            
            
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];
    }];

    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
